package com.sky.controller.admin;

import com.sky.dto.OrdersCancelDTO;
import com.sky.dto.OrdersConfirmDTO;
import com.sky.dto.OrdersPageQueryDTO;
import com.sky.dto.OrdersRejectionDTO;
import com.sky.mapper.OrderMapper;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.OrderService;
import com.sky.vo.OrderStatisticsVO;
import com.sky.vo.OrderVO;
import com.sky.vo.UserReportVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;

@RestController("adminOrderController")
@RequestMapping("/admin/order")
@Api(tags = "订单管理")
@Slf4j
public class OrderController {

    @Autowired
    private OrderService orderService;

    /**
     *  管理端订单搜索
     *  /admin/order/conditionSearch
     */
    @GetMapping("/conditionSearch")
    @ApiOperation("订单搜索")
    public Result<PageResult> conditionSearch(OrdersPageQueryDTO ordersPageQueryDTO ) {
        log.info("订单搜索,{}", ordersPageQueryDTO);

        PageResult page=orderService.pageQuery(ordersPageQueryDTO);
        return Result.success(page);
    }

    /**
     * 各个状态的订单数量统计
     *
     * @return
     */
    @GetMapping("/statistics")
    @ApiOperation("各个状态的订单数量统计")
    public Result<OrderStatisticsVO> statistics() {
        log.info("各个状态的订单数量统计");

        // OrderStatisticsVO 待派送,待接单，派送中
        OrderStatisticsVO orderStatisticsVO = orderService.statistics();
        return Result.success(orderStatisticsVO);
    }
    /**
     * 订单详情
     */
    @GetMapping("/details/{id}")
    @ApiOperation("订单详情")
    public Result<OrderVO> details(@PathVariable Long id) {
        log.info("订单详情,{}",id);

        OrderVO orderVO = orderService.orderDetail(id);
        return Result.success(orderVO);
    }

    /**
     * 商家接单
     * @param OrdersConfirmDTO 订单id
     */
    @PutMapping("/confirm")
    @ApiOperation("商家接单")
    public Result<String> accept(@RequestBody OrdersConfirmDTO OrdersConfirmDTO) {
        log.info("商家接单,{}",OrdersConfirmDTO);

        orderService.confirmOrder(OrdersConfirmDTO);
        return Result.success();
    }

    /**
     * 商家拒单
     * @param ordersRejectionDTO 订单id
     */
    @PutMapping("/rejection")
    @ApiOperation("商家拒单")
    public Result<String> rejecte(@RequestBody OrdersRejectionDTO ordersRejectionDTO) {
        log.info("商家拒单,{}",ordersRejectionDTO);

        orderService.rejectionOrder(ordersRejectionDTO);
        return Result.success();
    }

    /**
     * 商家取消
     * @param ordersCancelDTO 订单id
     *
     */
    @PutMapping("/cancel")
    @ApiOperation("取消订单")
    public Result<String> cancelOrder(@RequestBody OrdersCancelDTO ordersCancelDTO) throws Exception {
        log.info("取消订单：{}", ordersCancelDTO);

        orderService.adminCancel(ordersCancelDTO);
        return Result.success();
    }

    /**
     * 派送中
     */
    @PutMapping("/delivery/{id}")
    @ApiOperation("派送订单")
    public Result<String> delivery(@PathVariable Long id) {
        log.info("派送订单：{}", id);

        orderService.deliveryOrder(id);
        return Result.success();
    }
    /**
     * 订单完成
     */
    @PutMapping("/complete/{id}")
    @ApiOperation("订单完成")
    public Result<String> complete(@PathVariable Long id) {
        log.info("订单完成：{}", id);

        orderService.completeOrder(id);
        return Result.success();
    }




}
